package cn.hr.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 环绕增强类
 *
 * @author MO
 */
@Component
@Aspect
public class AroundLogger {
    private static final Logger LOGGER = LoggerFactory.getLogger(AroundLogger.class);

    @Around("execution(* cn.hr.service..*.*(..))")
    public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable {
        try {
            Object result = jp.proceed();
            StringBuilder log = new StringBuilder("调用:");
            log.append(jp.getTarget());
            log.append("的");
            log.append(jp.getSignature().getName());
            log.append("方法");
            LOGGER.debug(log.toString());
            return result;
        } catch (Throwable e) {
            e.printStackTrace();
            StringBuilder log = new StringBuilder(jp.getSignature().getName());
            log.append("方法发生异常!");
            LOGGER.debug(log.toString());
            throw e;
        } finally {
            StringBuilder log = new StringBuilder(jp.getSignature().getName());
            log.append("方法结束执行");
            LOGGER.debug(log.toString());
        }
    }
}
